using MODEL;
using System.Collections.Generic;
using System.Data;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Newtonsoft.Json;
using System.IO;
using Service.Main;
using GalaSoft.MvvmLight.CommandWpf;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using Common;

namespace ViewModel
{  
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel(IMainService service)
        {
            m_Service = service;
            MenuCommand = new RelayCommand<MenuInfo>(MenuCommand_Execute);
            InitData();
        }
        #region Service

        private IMainService m_Service;

        #endregion

        #region ViewProperties

        private List<MenuInfo> m_MenuInfos;

        public List<MenuInfo> MenuInfos
        {
            get { return m_MenuInfos; }
            set { m_MenuInfos = value; RaisePropertyChanged(() => MenuInfos); }
        }

        private MenuInfo m_CurrentMainMenu;

        public MenuInfo CurrentMainMenu
        {
            get { return m_CurrentMainMenu; }
            set { m_CurrentMainMenu = value; RaisePropertyChanged(() => CurrentMainMenu); }
        }

        private string m_CurrentViewUri;

        public string CurrentViewUri
        {
            get { return m_CurrentViewUri; }
            set { m_CurrentViewUri = value; RaisePropertyChanged(() => CurrentViewUri); }
        }

        #endregion

        #region Command
        public ICommand MenuCommand { get; set; }

        #endregion



        private async void InitData()
        {
            MenuInfos = await m_Service.GetMenuInfos();
            this.CurrentViewUri = "HomePage\\Index.xaml";
        }

        private void MenuCommand_Execute(MenuInfo menuInfo)
        {
            if (string.IsNullOrWhiteSpace(menuInfo.FormUrl))
            {
                MessengerInstance.Send("", "MainWindow_OpenSubMenu");
            }
            else
            {
                MessengerInstance.Send("", "MainWindow_CloseSubMenu");
                CurrentViewUri = menuInfo.FormUrl;
            }
        }
    }
}